﻿Imports System
Imports System.ComponentModel
Imports System.Globalization
Imports System.Security.Permissions
Imports System.Workflow.Activities.Rules
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Activities.Rules.Design
    Friend Class RuleConditionReferenceNamePropertyDescriptor
        Inherits DynamicPropertyDescriptor
        ' Methods
        Public Sub New(ByVal serviceProvider As IServiceProvider, ByVal descriptor As PropertyDescriptor)
            MyBase.New(serviceProvider, descriptor)
        End Sub

        Public Overrides Function GetEditor(ByVal editorBaseType As Type) As Object
            New SecurityPermission(PermissionState.Unrestricted).Demand
            Return New ConditionNameEditor
        End Function

        Public Overrides Function GetValue(ByVal component As Object) As Object
            If (component Is Nothing) Then
                Throw New ArgumentNullException("component")
            End If
            Dim reference As RuleConditionReference = [TryCast](component,RuleConditionReference)
            If (reference Is Nothing) Then
                Throw New ArgumentException(String.Format(CultureInfo.CurrentCulture, Messages.NotARuleConditionReference, New Object() { "component" }), "component")
            End If
            If (Not reference.ConditionName Is Nothing) Then
                Return reference.ConditionName
            End If
            Return Nothing
        End Function

        Public Overrides Sub SetValue(ByVal component As Object, ByVal value As Object)
            If (component Is Nothing) Then
                Throw New ArgumentNullException("component")
            End If
            If Not TypeOf component Is RuleConditionReference Then
                Throw New ArgumentException(String.Format(CultureInfo.CurrentCulture, Messages.NotARuleConditionReference, New Object() { "component" }), "component")
            End If
            Dim key As String = [TryCast](value,String)
            If ((key Is Nothing) OrElse (key.TrimEnd(New Char(0  - 1) {}).Length = 0)) Then
                key = String.Empty
            End If
            Dim serviceProvider As ISite = PropertyDescriptorUtils.GetSite(MyBase.ServiceProvider, component)
            If (serviceProvider Is Nothing) Then
                Throw New InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Messages.MissingService, New Object() { GetType(ISite).FullName }))
            End If
            Dim conditions As RuleConditionCollection = Nothing
            Dim definitions As RuleDefinitions = ConditionHelper.Load_Rules_DT(serviceProvider, Helpers.GetRootActivity([TryCast](serviceProvider.Component,Activity)))
            If (Not definitions Is Nothing) Then
                conditions = definitions.Conditions
            End If
            If (((Not conditions Is Nothing) AndAlso (key.Length <> 0)) AndAlso Not conditions.Contains(key)) Then
                Dim item As New RuleExpressionCondition { _
                    .Name = key _
                }
                conditions.Add(item)
                ConditionHelper.Flush_Rules_DT(serviceProvider, Helpers.GetRootActivity([TryCast](serviceProvider.Component,Activity)))
            End If
            Dim propertyDescriptor As PropertyDescriptor = TypeDescriptor.GetProperties(component).Item("ConditionName")
            If (Not propertyDescriptor Is Nothing) Then
                PropertyDescriptorUtils.SetPropertyValue(serviceProvider, propertyDescriptor, component, key)
            End If
        End Sub


        ' Properties
        Public Overrides ReadOnly Property Description As String
            Get
                Return Messages.NamePropertyDescription
            End Get
        End Property

        Public Overrides ReadOnly Property IsReadOnly As Boolean
            Get
                Return False
            End Get
        End Property

    End Class
End Namespace

